/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//http://stackoverflow.com/questions/5462583/poker-side-pot-algorithm
package extension.game.poker;

import java.util.List;
import log.Debug;

/**
 *
 * @author daz
 */
public final class PotManager {

    private long[] potContribute;
    private int[] markWinners;
    private int potAmount;
    private boolean continueSplit;

    public PotManager() {
        potContribute = new long[PokerGame.MAX_PLAYER_JOIN];
        markWinners = new int[PokerGame.MAX_PLAYER_JOIN];
        reset();
    }

    public void betAddPot(int playerId, long money) {
        if (potContribute[playerId] == -1) {
            potContribute[playerId] = money;
        } else {
            potContribute[playerId] += money;
        }
        Debug.d("contribute pot : id" + playerId + "-money:" + potContribute[playerId]);
        potAmount += money;
    }

    public void setPotContribute(long[] potContribute) {
        this.potContribute = potContribute;
        potAmount = 0;
        for (int i = 0; i < potContribute.length; i++) {
            potAmount += potContribute[i];
        }
        Debug.console("pot amount :" + potAmount);
    }

    public int generateMoneyWin(List<PokerPlayer> listWinners) {
        int numberWinners = listWinners.size();
        long[] winnerContribute = new long[numberWinners];
        for (int i = 0; i < numberWinners; i++) {
            winnerContribute[i] = potContribute[listWinners.get(i).getSeatPos()];
            Debug.d(listWinners.get(i).getName() + ":" + listWinners.get(i).getSeatPos());
            Debug.d(listWinners.get(i).getName() + ":" + winnerContribute[i]);
        }
        for (int i = 0; i < numberWinners - 1; i++) {
            for (int j = i + 1; j < numberWinners; j++) {
                if (winnerContribute[j] < winnerContribute[i]) {
                    long temp = winnerContribute[i];
                    winnerContribute[i] = winnerContribute[j];
                    winnerContribute[j] = temp;

                }
            }
        }
        int i = 0;
        do {
            int pot = 0;
            Debug.i("get each player :" + winnerContribute[i]);
            for (int j = 0; j < PokerGame.MAX_PLAYER_JOIN; j++) {
                Debug.d("contriubte :" + potContribute[j] + "," + j);
                if (potContribute[j] != -1) {
                    if (winnerContribute[i] >= potContribute[j]) {
                        pot += potContribute[j];
                        potContribute[j] = 0;
                    } else {
                        pot += winnerContribute[i];
                        potContribute[j] -= winnerContribute[i];
                    }
                }
            }
            //update player money
            for (PokerPlayer p : listWinners) {
                p.updateAddMoney(pot / numberWinners);
            }
            int r = pot % numberWinners;
            int k = 0;
            while (r > 0) {
                listWinners.get(k).updateAddMoney(1);
                k++;
                r--;
            }
//            listWinners.remove(0);
            numberWinners--;
            i++;
            Debug.d("pot :" + pot);
            Debug.d("number win :" + numberWinners);
            potAmount -= pot;
        } while (numberWinners > 0);
        Debug.d("pot amount:" + potAmount);
        continueSplit = (potAmount > 0 ? true : false);
        return listWinners.size();
    }

    public void reset() {
        for (int i = 0; i < PokerGame.MAX_PLAYER_JOIN; i++) {
            potContribute[i] = -1;
            markWinners[i] = 0;
        }
        potAmount = 0;
    }

    public void setPotAmount(int potAmount) {
        this.potAmount = potAmount;
    }

    public int getPotAmount() {
        return potAmount;
    }

    public boolean isContinueSplit() {
        return continueSplit;
    }
}
